<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改在线实验项目')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-experiment-edit" th:object="${tbExperiment}">
            <input name="experimentId" id="experimentId" th:field="*{experimentId}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">实验名称：</label>
                <div class="col-sm-8">
                    <input name="experimentName" id="experimentName" th:field="*{experimentName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">实验链接：</label>
                <div class="col-sm-8">
                    <input name="experimentHref" id="experimentHref" th:field="*{experimentHref}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">封面上传：</label>
                <!--前端如何显示回显图片？？-->
                <div class="col-sm-8">
                    <div class="form-group">
                        <div class="file-loading">
                            <input id="file" type="file" multiple name="file">
                            <input th:field="*{experimentPhoto}" id="experimentPhoto" hidden="hidden"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">实验说明：</label>
                <div class="col-sm-8">
                    <input name="experimentExplain" id="experimentExplain" th:field="*{experimentExplain}" class="form-control" type="hidden">
                    <div class="form-group">
                        <div id="summernote1">

                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group" hidden="hidden">
                <label class="col-sm-3 control-label">实验报告：</label>
                <div class="col-sm-8">
                    <input name="experimentReport" id="experimentReport" th:field="*{experimentReport}" class="form-control" type="hidden">
                    <div class="form-group">
                        <div id="summernote2">

                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">是否开放：</label>
                <div class="col-sm-8">
                    <!--<input name="ifOpen" th:field="*{ifOpen}" class="form-control" type="text">-->
                    <select name="ifOpen" class="form-control" th:field="*{ifOpen}">
                        <option value="1">开放</option>
                        <option value="2">不开放</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/experiment";
        $("#form-experiment-edit").validate({
            focusCleanup: true
        });

        $('#summernote1').summernote({
            placeholder: '请输入实验报告模板',
            height: 500,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function(files, editor, $editable) {
                    var formData = new FormData();
                    formData.append("file", files[0]);
                    $.ajax({
                        type: "POST",
                        url: ctx + "common/upload",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code == web_status.SUCCESS) {
                                //alert(result.url+","+result.fileName)
                                $('.summernote2').summernote('editor.insertImage', result.url, result.fileName);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function(error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });


        $('#summernote2').summernote({
            placeholder: '请输入实验报告模板',
            height: 500,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function(files, editor, $editable) {
                    var formData = new FormData();
                    formData.append("file", files[0]);
                    $.ajax({
                        type: "POST",
                        url: ctx + "common/upload",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code == web_status.SUCCESS) {
                                //alert(result.url+","+result.fileName)
                                $('.summernote2').summernote('editor.insertImage', result.url, result.fileName);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function(error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });


        /*初始化实验说明和实验报告*/
        if ($("#experimentExplain").val().length > 0) {
            $('#summernote1').summernote('code', $("#experimentExplain").val());
        }
        if ($("#experimentReport").val().length > 0) {
            $('#summernote2').summernote('code', $("#experimentReport").val());
        }

        /**
         * 文件上传以及图片回显
         */
        //格式化文件上传
        $('#file').fileinput({
            allowedFileExtensions: ['jpg', 'png', 'gif'],//接收的文件后缀,
            enctype: 'multipart/form-data',
            showBrowse: true,
            showUpload: false, //是否显示上传按钮
            showCaption: false,//是否显示标题
            browseClass: "btn btn-success", //按钮样式
            previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            initialPreviewAsData: true,
            initialPreviewFileType: 'image',
            initialPreview: ctx + $("#experimentPhoto").val(), //要显示的图片的路径
            //msgFilesTooMany: "选择上传的文件数量({2}) 超过允许的最大数值{2}！",
        });


        function submitHandler() {
            if ($.validate.form()) {
                var formData = new FormData();
                formData.append("experimentId",$("#experimentId").val())
                formData.append("experimentName",$("#experimentName").val())
                formData.append("experimentHref",$("#experimentHref").val())
                formData.append("experimentExplain",$("#summernote1").summernote("code"))
                formData.append("experimentReport",$("#summernote2").summernote("code"))
                if ($("#file").val()!=null || $("#file").val!=undefined) {
                    formData.append("file", $("#file")[0].files[0]);
                }
                //formData.append("file",$("#file")[0].files[0])
                formData.append("ifOpen",$("#ifOpen").val())
                if ($.validate.form()) {
                    $.ajax({
                        url: prefix + "/edit",
                        type: "POST",
                        data : formData,
                        cache: false,
                        processData: false,
                        contentType: false,
                        success: function (data) {
                            console.log(data)
                            if(data.code == 0){
                                $.modal.msgReload(data.msg,'success')
                            }else{
                                $.modal.msgReload(data.msg,'error')
                            }
                        }
                    })
                }
                //$.operate.save(prefix + "/edit", $('#form-experiment-edit').serialize());
            }
        }

    </script>
</body>
</html>